package com.wlx.controller;

import com.wlx.pojo.Emp;
import com.wlx.pojo.Result;
import com.wlx.service.EmpService;
import com.wlx.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ：羽若（1292715325@qq.com）
 * @date ：Created in 2024/7/5 16:55
 * @description：
 * @modified By：
 * @version:
 */
@RestController
public class LoginController {

    @Autowired
    private EmpService empService;

    @PostMapping("/login")
    public Result login(@RequestBody Emp emp){
        Emp e = empService.login(emp);


        //登录成功
        if (e != null){
            Map<String, Object> claims = new HashMap<>();
            claims.put("id",e.getId());
            claims.put("name", e.getName());
            claims.put("username", e.getUsername());

            String jwt = JwtUtils.generateJwt(claims);
            return Result.success(jwt);
        }

        //登录失败
        return Result.error("账号或密码错误！");
    }
}
